/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	import virtPresenter.business.LecturesDelegate;
	import virtPresenter.business.VideoXMLDelegate;
	import virtPresenter.event.LoadMouseXMLEvent;
	import virtPresenter.event.LoadSearchXMLEvent;
	import virtPresenter.event.LoadVideoXMLEvent;
	import virtPresenter.model.virtPresenterLocator;	
	
	/**
	 * Commands handles a LoadVideoXMLEvent and loads the video.xml
	 * for the folder, given by the event.	
	 */	
	public class LoadVideoXMLCommand implements ICommand, IResponder
	{	
		protected var model:virtPresenterLocator=virtPresenterLocator.getInstance();
		
		/**
		 * Execute method is invoked by the LoadVideoXMLEvent. Calls
		 * its delegate to recieve the result of the video.xml file.
		 * 
		 * @parameters event LoadVideoXMLEvent
		 * 
		 * @see virtPresenter.event.LoadVideoXMLEvent
		 */
		public function execute(event:CairngormEvent):void
		{
			var folder:String = LoadVideoXMLEvent(event).folder;				
			var delegate:VideoXMLDelegate = new VideoXMLDelegate(this);
			delegate.getVideoURL(folder);		
		}
		
		/**
		 * Result method is called by the VideoXMLDelegate and stores the results
		 * of search.xml as an ArrayCollection in the model. 
		 * Throws a LoadSearchXMLEvent.
		 *  
		 * @parameters data Data Object that contains the results of the request to the video.xml
		 * 
		 * @see virtPresenter.business.VideoXMLDelegate
		 */
		public function result(data:Object):void
		{
			trace("LoadVideoXMLCommand. Video.xml loaded: "+data.result.video.url);
			model.flv = data.result.video.url;
			model.uri = data.result.video.red5URL;
			CairngormEventDispatcher.getInstance().dispatchEvent(new LoadMouseXMLEvent(model.lectureFolder));		
		}
		
		/**
		 * Fault method is invoked by VideoXMLDelegate if an error occured
		 * loading the video.xml file.
		 * 
		 * @parameters info Info object that contains information about the fault
		 * 
		 * @see virtPresenter.business.VideoXMLDelegate  
		 */
		public function fault(info:Object):void
		{
			trace("LoadVideoXMLCommand. Fault in loading video.xml:"+info.toString());
		}
	}
}